from sqlalchemy import Column, String, Integer, Boolean, DateTime, Date
from sqlalchemy.orm import declarative_base

Base = declarative_base()


class ParameterConfigDtl(Base):
    __tablename__ = 'wops_parameter_config_dtl'

    id = Column(String(32), primary_key=True, comment='编号')
    name = Column(String(32), nullable=False, comment='名称')
    code = Column(String(32), nullable=False, comment='编码')
    value = Column(String(32), nullable=False, comment='值')
    remark = Column(String(128), comment='备注')
    add_date = Column(DateTime, comment='新增时间')
    edit_date = Column(DateTime, comment='修改时间')
    parameter_configgroup_code = Column(String(32), nullable=False, comment='关联表')


class WorkType(Base):
    # 定义表名
    __tablename__ = 'wops_work_type'

    # 定义字段
    id = Column(String(32), primary_key=True, comment='编号')
    name = Column(String(32), nullable=False, comment='loc')
    work_type = Column(Integer, nullable=False, comment='工作类型')
    origin_pack_type = Column(Integer, nullable=False, comment='1板、2纸箱、3周转箱、4纸箱周转箱')
    target_pack_type = Column(Integer, nullable=False, comment='1板、2纸箱、3周转箱、4纸箱周转箱')
    operate_pack_type = Column(Integer, nullable=False, comment='1板、2纸箱、3周转箱、4箱式、5MPQ（最小分拣单位）')


class WopsWaveletOrder(Base):
    __tablename__ = 'wops_wavelet_order'

    id = Column(String(32), primary_key=True, comment='编号')
    plan_begin = Column(DateTime, nullable=False, comment='计划开始时间')
    plan_end = Column(DateTime, nullable=False, comment='计划结束时间')
    state = Column(Integer, default=None, comment='状态：1.新建、2进行中、3已完成')
    work_date = Column(Date, nullable=False, comment='工作日')
    delivery_time_windowid = Column(String(32), nullable=False)
    seq = Column(Integer, default=None, comment='小波次顺序')

    __table_args__ = ({"comment": "小波次执行时间窗（小波次）"},)


class SelfProductVehiclePlan(Base):
    """
    自制发车表
    """
    __tablename__ = 'wops_self_product_vehicle_plan'

    id = Column(String(32), primary_key=True, nullable=False, comment='编号')
    work_date = Column(Date, nullable=False, comment='工作日')
    plan_begin = Column(DateTime, nullable=False, comment='计划开始时间')
    plan_end = Column(DateTime, nullable=False, comment='计划结束时间')
    deliver_factory = Column(String(32), nullable=True, comment='交付工厂')
    deliver_factory_code = Column(String(32), nullable=False, comment='交付工厂编码')
    vehicle_num = Column(Integer, nullable=False, comment='自制发车数')


class VehicleTask(Base):
    """
    发车任务
    """
    __tablename__ = 'wops_vehicle_task'

    id = Column(String(32), primary_key=True, nullable=False, comment='编号')
    plan_begin = Column(DateTime, nullable=False, comment='计划开始时间')
    plan_end = Column(DateTime, nullable=False, comment='计划结束时间')
    deliver_factory = Column(String(32), nullable=False, comment='交付工厂')
    deliver_factory_code = Column(String(32), nullable=False, comment='交付工厂编码')
    ems_case_deliver_port_num = Column(String(32), comment='可同时占用交货口:发ems箱形态时可同时占用口')
    ems_pallet_deliver_port_num = Column(String(32), comment='可同时占用交货口:发栈板形态时出口情况')
    work_date = Column(Date, comment='工作日')
    pallet_begin = Column(DateTime, nullable=False, comment='栈板出货开始时间')
    pallet_end = Column(DateTime, nullable=False, comment='栈板出货结束时间')
    box_begin = Column(DateTime, nullable=False, comment='箱式出货开始时间')
    box_end = Column(DateTime, nullable=False, comment='箱式出货结束时间')

    def ems_port_nums(self) -> int:
        """
        EMS/负一自制 出货口分配 数量
        示例 110
        - 三位表示: 3个出货口
        - 1: 表示分配出户口
        :return:
        """
        if self.ems_case_deliver_port_num:
            return self.ems_case_deliver_port_num.count('1')
        return 0
